Ext.define('iMedproplus.view.patient.PatientGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.patientgridpanel',
    store: 'iMedproplus.store.Patient',
    id: 'patient-gridpanel',
    rowEditor : null,
    autoScroll: true,
    
    initComponent: function() {
        var me = this;
        me.rowEditor = Ext.create('Ext.grid.plugin.RowEditing', {
            clicksToEdit: 2,
            listeners: {
                canceledit: {
                    fn: me.onCancelEdit,
                    scope: me
                },
                edit: {
                    fn: me.onUpdateEdit,
                    scope: me
                }
            }
        });
        Ext.applyIf(me, {
            columns: [
            {
                xtype: 'gridcolumn',
                dataIndex: 'pesel',
                text: 'PESEL',
                //flex: 1,
                editor: {
                    xtype: 'textfield',
                    allowBlank: false
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'firstName',
                text: 'Imię',
                //flex: 1 ,                   
                editor: {
                    xtype: 'textfield'
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'lastName',
                text: 'Nazwisko',
                //flex: 1,                   
                editor: {
                    xtype: 'textfield'
                }
            },
            {
                xtype: 'datecolumn',
                dataIndex: 'dateOfBirth',
                text: 'Data urodzenia',
                //flex: 1,                   
                editor: {
                    xtype: 'datefield'
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'street',
                text: 'Ulica',
                //flex: 1,                   
                editor: {
                    xtype: 'textfield'
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'city',
                text: 'Miasto',
                //flex: 1,                   
                editor: {
                    xtype: 'textfield'
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'post',
                text: 'Poczta',
                //flex: 1,                   
                editor: {
                    xtype: 'textfield'
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'postCOde',
                text: 'Kod pocztowy',
                //flex: 1,                   
                editor: {
                    xtype: 'textfield'
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'phone',
                text: 'Telefon',
                //flex: 1,                   
                editor: {
                    xtype: 'textfield'
                }
            },
            {
                xtype: 'numbercolumn',
                dataIndex: 'paymentInstitution',
                text: 'P\u0142atnik',
                //flex: 1,                   
                editor: {
                    xtype: 'numberfield'
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'email',
                text: 'E-mail',
                //flex: 1,                   
                editor: {
                    xtype: 'textfield'
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'insuranceNumber',
                text: 'Numer ubezpieczenia',
                //flex: 1,                   
                editor: {
                    xtype: 'textfield'
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'insuranceType',
                text: 'Rodzaj ubezpieczenia',
                //flex: 1,                   
                editor: {
                    xtype: 'textfield'
                }
            }
                
            ],
            viewConfig: {
            },
            dockedItems: [
            {
                xtype: 'toolbar',
                dock: 'top',
                items: [
                {
                    xtype: 'button',
                    text: 'Dodaj',
                    icon: './resources/icons/add.png',
                    listeners : {
                        click : {
                            fn : me.onPatientAddButtonClick,
                            scope: me
                        }
                    }
                },
                {
                    xtype: 'button',
                    text: 'Usuń',
                    icon: './resources/icons/delete.png',
                    listeners: {
                        click: {
                            fn : me.onPatientRemoveButtonClick,
                            scope: me
                        }
                    }
                },  
                {
                    xtype: 'button',
                    text: 'Zapisz',
                    icon: './resources/icons/database_save.png',
                    listeners: {
                        click: {
                            fn : me.onPatientSaveButtonClick,
                            scope: me
                        }
                    }
                },
                {
                    xtype: 'button',
                    text: 'Anuluj',
                    icon: './resources/icons/cancel.png',
                    listeners: {
                        click: {
                            fn : me.onPatientCancelButtonClick,
                            scope: me
                        }
                    }
                }]
            }
            ],
            plugins:  [ me.rowEditor],
            selModel: Ext.create('Ext.selection.RowModel', {
                mode: 'SINGLE'
            }),

            listeners: {
                selectionchange: {
                    fn: me.onGridpanelSelectionChange,
                    scope: me
                }
            }
        });

        me.callParent(arguments);
    },
    
    onPatientAddButtonClick: function(){
        this.fireEvent('itemAdd', this.rowEditor);
    },
    
    onCancelEdit: function(grid){
        this.fireEvent('itemCancelEdit', grid);
    },
    
    onUpdateEdit: function() {
        this.fireEvent('itemUpdate');
    },
    
    onPatientRemoveButtonClick: function(){
        var grid = Ext.getCmp('patient-gridpanel');
        this.fireEvent('itemRemove', grid.getSelectionModel());
    },
    
    onPatientSaveButtonClick: function(){
        this.fireEvent('itemSave');
    },
    onGridpanelSelectionChange: function(tablepanel, selections, options){
        this.fireEvent('organizationStructureSelectionChange', selections);
    },
    onPatientCancelButtonClick: function(){
        Ext.App.getStore('iMedproplus.store.Patient').load();
    }

});